package com.sphd.emanu.activity.pointconsume;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.market.Points;
import com.sphd.emanu.pojo.market.PointsQueryVo;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.BigDisMailUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @Author Hu
 * @Time 2017/6/22 -- 11:48.
 * @Email 87683202@qq.com
 */
public class PointconsumeActivity extends MVPBaseActivity<PointconsumeContract.View, PointconsumePresenter> implements PointconsumeContract.View {


    Intent intent;
    Bundle bundle;
    PointsQueryVo pointsQueryVo;
    Points points2;
    Points points3;
    AndroidNextInputs inputs;
    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.ready_money)
    MaterialEditText readyMoney;
    @BindView(R.id.edit_ready_pointe)
    MaterialEditText editReadyPointe;


    @BindView(R.id.tv_first)
    TextView tvFirst;
    @BindView(R.id.edit_topup_com)
    MaterialEditText editTopupCom;
    @BindView(R.id.second)
    TextView second;
    @BindView(R.id.edit_com_pointe)
    MaterialEditText editComPointe;
    @BindView(R.id.layout_ready_show)
    LinearLayout layoutReadyShow;
    @BindView(R.id.layout_topup_show)
    LinearLayout layoutTopupShow;
    @BindView(R.id.switch_ready_money_com)
    Switch switchReadyMoneyCom;
    @BindView(R.id.switch_topup_com)
    Switch switchTopupCom;
    List<Moudle> moudles;
    boolean isCanDo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pointconsume);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        switchReadyMoneyCom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    layoutReadyShow.setVisibility(View.VISIBLE);
                } else {
                    layoutReadyShow.setVisibility(View.GONE);
                }
            }
        });
        switchTopupCom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    layoutTopupShow.setVisibility(View.VISIBLE);
                } else {
                    layoutTopupShow.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "消费积分设置");
        inputs = new AndroidNextInputs();
        intent = getIntent();
        bundle = intent.getExtras();
        pointsQueryVo = (PointsQueryVo) bundle.getSerializable(Constant.POINTS_QUERY_VO);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_MAKETING_4);
        if (!isCanDo){
            btnSave.setVisibility(View.GONE);
        }
        for (Points points : pointsQueryVo.getPointsList()) {
            if (points.getType().equals("2")) {
                this.points2 = points;
            } else if (points.getType().equals("3")) {
                this.points3 = points;
            }
        }
        switchReadyMoneyCom.setChecked(points2.getEnable());
        if (switchReadyMoneyCom.isChecked()) {
            layoutReadyShow.setVisibility(View.VISIBLE);
        } else {
            layoutReadyShow.setVisibility(View.GONE);
        }

        switchTopupCom.setChecked(points3.getEnable());
        if (switchTopupCom.isChecked()) {
            layoutTopupShow.setVisibility(View.VISIBLE);
        } else {
            layoutTopupShow.setVisibility(View.GONE);
        }

        readyMoney.setText(BigDisMailUtils.subZeroAndDot(points2.getAmount().toString()));
        editReadyPointe.setText(String.valueOf(points2.getPonits()));
        editTopupCom.setText(BigDisMailUtils.subZeroAndDot(points3.getAmount().toString()));
        editComPointe.setText(String.valueOf(points3.getPonits()));

        inputs.add(readyMoney, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));
        inputs.add(editReadyPointe, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));
        inputs.add(editTopupCom, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));
        inputs.add(editComPointe, StaticScheme.NotBlank().msgOnFail("不能为空"), StaticScheme.Numeric().msgOnFail("请填入整数"));


    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        if (!switchReadyMoneyCom.isChecked() && !switchTopupCom.isChecked()) {
            showProgressDialog();
            mPresenter.updatePayPoints(points2.getId(), switchReadyMoneyCom.isChecked(), "2", null, null,
                    points3.getId(), switchReadyMoneyCom.isChecked(), "3", null, null);
        } else {
            if (!switchReadyMoneyCom.isChecked()) {
                inputs.remove(readyMoney);
                inputs.remove(editReadyPointe);
            }
            if (!switchTopupCom.isChecked()) {
                inputs.remove(editTopupCom);
                inputs.remove(editComPointe);
            }
            if (inputs.test()) {
                showProgressDialog();
                mPresenter.updatePayPoints(points2.getId(), switchReadyMoneyCom.isChecked(), "2", switchReadyMoneyCom.isChecked() ?
                                BigDisMailUtils.turn(readyMoney.getText().toString().trim(), Constant.POINT) : null,
                        switchReadyMoneyCom.isChecked() ? Integer.valueOf(editReadyPointe.getText().toString().trim()) : null,
                        points3.getId(), switchTopupCom.isChecked(), "3", switchTopupCom.isChecked() ?
                                BigDisMailUtils.turn(editTopupCom.getText().toString().trim(), Constant.POINT) : null,
                        switchTopupCom.isChecked() ? Integer.valueOf(editComPointe.getText().toString().trim()) : null);

            } else {
                toast("请按提示要求填写内容");
            }


        }

    }

    @Override
    public void recRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast(R.string.up_success);
            setResult(Constant.REQUEST_CODE);
            finish();
        } else {
            toast(R.string.up_failure);
        }
    }
}
